#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> 
using namespace std;
 
long long gcd(long a,long b){
    return b==0?a:gcd(b,a%b);
}
int main(){
    long long T;
    scanf("%I64d",&T);
    while(T--)
	{
        long long n;
        scanf("%I64d",&n);
        int a[n+2];
        for(int i = 1 ;i <= n;i++)scanf("%d",&a[i]);
        long long res = 1;
        for(int i = 1 ;i <= n;i++)
		{
            long long tmp = 1;
            long long pos = a[i];
            while(pos!=i)
			{
                tmp++;
                pos = a[pos];
            }
            res = res * tmp /gcd(res,tmp);
        }
        printf("%I64d\n",res);
    }
    return 0;
}

